package cn.zxf.drools.demo.controller;

import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.zxf.drools.demo.model.Address;
import cn.zxf.drools.demo.model.fact.AddressCheckResult;

import javax.annotation.Resource;

@RequestMapping( "/test" )
@Controller
public class TestController {

    @Resource
    private KieContainer kieContainer;

    @ResponseBody
    @RequestMapping( "/address" )
    public void test( int num ) {
        Address address = new Address();
        address.setPostcode( generateRandom( num ) );
        KieSession kieSession = kieContainer.newKieSession();

        AddressCheckResult result = new AddressCheckResult();
        kieSession.insert( address );
        kieSession.insert( result );
        int ruleFiredCount = kieSession.fireAllRules();
        kieSession.destroy();
        System.out.println( "触发了" + ruleFiredCount + "条规则" );

        if ( result.isPostCodeResult() ) {
            System.out.println( "规则校验通过" );
        }

    }

    /**
     * 生成随机数
     * 
     * @param num
     * @return
     */
    public String generateRandom( int num ) {
        String chars = "0123456789";
        StringBuffer number = new StringBuffer();
        for ( int i = 0; i < num; i++ ) {
            int rand = (int) ( Math.random() * 10 );
            number = number.append( chars.charAt( rand ) );
        }
        return number.toString();
    }
}
